package yxy.game.pm2.module.conquest;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.module.RedisLock;

public class ConquestDynamicModule extends PlayerModule {

    // bean===========================================================================

    private int times;//当天爬塔次数

    private int degree;//难度等级   规则：每40关提升一级，第二天减少以及

    private DateTime time;// 时间标记

    public int getTimes() {
        return times;
    }

    public void setTimes(int times) {
        this.times = times;
    }

    public int getDegree() {
        return degree;
    }

    public void setDegree(int degree) {
        this.degree = degree;
    }

    public DateTime getTime() {
        return time;
    }

    public void setTime(DateTime time) {
        this.time = time;
    }

    // redis==========================================================================
    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:CONQUESTDYNAMIC", playerCode);
    }

    //更新
    private void update() {
        DateTime now = DateTime.now();
        if (getTime() == null || !new LocalDate(now).equals(new LocalDate(getTime()))) {
            try {
                if (tryLock()) {
                    //下次登录如果是下一天就更新
                    setTime(now);
                    setTimes(0);
                    if (getDegree() > 0) setDegree(getDegree() - 1);
                    this.set();
                }
            } catch (Exception e) {
            } finally {
                unlock();
            }
        }
    }

    //缓存
    public void set() {
        try {
            String key = key(getPlayerCode());
            setv(key, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    static public ConquestDynamicModule get(String playerCode) {
        try {
            String key = key(playerCode);
            ConquestDynamicModule module = getv(key);
            //todo 后续缓存加到数据库

            if (module == null) {
                module = create(playerCode);
            }

            module.update();
            return module;
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    //第一次初始化
    static private ConquestDynamicModule create(String playerCode) {
        DateTime now = DateTime.now();
        ConquestDynamicModule module = new ConquestDynamicModule();
        module.setPlayerCode(playerCode);
        module.setDegree(0);
        module.setTime(now);
        module.setTimes(0);
        module.set();
        return module;
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {

    }

    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

    public static RedisLock lock(String playerCode) {
        return getLock(String.format("lock:%s", key(playerCode)));
    }
}
